package com.sch.wanandroid.ui.login

import android.os.Bundle
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.coder.zzq.smartshow.toast.SmartToast
import com.sch.wanandroid.R
import com.sch.wanandroid.base.BaseActivity
import com.sch.wanandroid.constants.Constants
import com.sch.wanandroid.databinding.ActivityLoginBinding
import com.sch.wanandroid.event.LoginEvent
import com.sch.wanandroid.ui.regist.RegistActivity
import com.tencent.mmkv.MMKV
import kotlinx.android.synthetic.main.activity_login.*
import org.greenrobot.eventbus.EventBus

/**
 * Created by Sch.
 * Date: 2020/4/24
 * description:登录
 */
class LoginActivity : BaseActivity() {
    private lateinit var loginViewModel: LoginViewModel
    private lateinit var databinding: ActivityLoginBinding

    /**
     * 是否显示明文
     */
    private var isPasswordShow = false

    override fun init(savedInstanceState: Bundle?) {
        initTitle("登录")
        loginViewModel = ViewModelProvider(this).get(LoginViewModel::class.java)

        databinding.data = loginViewModel

        addListener()
        loginViewModel.loginSuccessLiveData.observe(this, Observer {
            if (it) {
                loginSuccess()
            }
        })
        loginViewModel.errorMsg.observe(this) {
            onError(it)
        }
    }

    private fun addListener() {
        databinding.llLogin.setOnClickListener {
            val userName = databinding.etUsername.text.toString()
            val passWord = databinding.etPassword.text.toString()
            when {
                userName.isEmpty() -> {
                    etUsername.setError("账号不能为空")
                }
                passWord.isEmpty() -> {
                    etPassword.setError("密码不能为空")
                }
                else -> {
                    setViewStatus(false)
                    loginViewModel.doLogin()
                }
            }
        }
        databinding.ivClearUserName.setOnClickListener {
            etUsername.requestFocus()
            etUsername.setText("")
        }
        databinding.ivPasswordVisibility.setOnClickListener {
            databinding.etPassword.requestFocus()
            if (isPasswordShow) {
                isPasswordShow = false
                databinding.ivPasswordVisibility.setImageResource(R.mipmap.password_hide)
                databinding.etPassword.transformationMethod =
                    PasswordTransformationMethod.getInstance()
            } else {
                isPasswordShow = true
                ivPasswordVisibility.setImageResource(R.mipmap.password_show)
                etPassword.transformationMethod = HideReturnsTransformationMethod.getInstance()
            }
            databinding.etPassword.setSelection(databinding.etPassword.text.length)
        }

        databinding.tvRegistAccont.setOnClickListener {
            intent(RegistActivity::class.java, false)
        }

    }

    /**
     * 登录时所有控件变为不可点击
     */
    private fun setViewStatus(lockStatus: Boolean) {
        llLogin.isEnabled = lockStatus
        etUsername.isEnabled = lockStatus
        etPassword.isEnabled = lockStatus
        ivPasswordVisibility.isEnabled = lockStatus
        ivClearUserName.isEnabled = lockStatus
        tvRegistAccont.isEnabled = lockStatus
        if (lockStatus) {
            tvLoginTxt.visibility = View.VISIBLE
            indicatorView.visibility = View.GONE
            indicatorView.hide()
        } else {
            tvLoginTxt.visibility = View.GONE
            indicatorView.visibility = View.VISIBLE
            indicatorView.show()
        }
    }

    override fun getLayoutId(): Int {
        databinding = DataBindingUtil.setContentView(this, R.layout.activity_login)
        databinding.lifecycleOwner = this
        return 0
    }

    private fun loginSuccess() {
        MMKV.defaultMMKV().encode(Constants.LOGIN, true)
        EventBus.getDefault().post(LoginEvent())
        finish()
    }

    fun onError(ex: String?) {
        setViewStatus(true)
        SmartToast.fail(ex)
    }

}